// Global settings
moveMap.bindCmd(keyboard, "o", "commandToServer('Operate');", "");

$OPERATE::REACH = 1.5; // how close we have to be to use something




// Global trigger function
// This will allow the player to use the 'o' key to 'Operate' objects in the game, which we define below
function serverCmdOperate(%client)
{
	// This is just an example of how we could search for multiple object types using bitmasks if needed
	%mask = (
		//$TypeMasks::MoveableObjectType |
		//$TypeMasks::ForceFieldObjectType |
		//$TypeMasks::ItemObjectType |
		//$TypeMasks::StaticShapeObjectType |
		$TypeMasks::ShapeBaseObjectType);

	// Get the location of the player's eye, and extend a cone vector out from it using container ray cast
	// See if we find a ShapeBaseObjectType
	%player = %client.player;
	%eye = %player.getEyeVector();
	%vec = VectorScale(%eye, $OPERATE::REACH);
	%start = %player.getEyePoint();
	%end = VectorAdd(%start, %vec);
	%found = ContainerRayCast(%start, %end, %mask, %player);

	// Call Operate() on the object found
	// We could check the class of the object to ensure that it's a class we want to call this function on, but I'm lazy
	if (%found)
		%found.getDataBlock().Operate(%found);
}




// Various sounds
datablock AudioProfile(JailActivated)
{
	filename = "~/data/shapes/jail/activate.wav";
	description = AudioClose3d;
	preload = true;
};

//datablock AudioProfile(JailDeactivated)
//{
//	filename = "~/data/shapes/jail/activate.wav";
//	description = AudioClose3d;
//	preload = true;
//};

datablock AudioProfile(JailShortCircuit)
{
	filename = "~/data/sound/shortcircuit.wav";
	description = AudioClose3d;
	preload = true;
};

datablock AudioProfile(JailPanelRemove)
{
	filename = "~/data/sound/metalplate.wav";
	description = AudioClose3d;
	preload = true;
};






// Wire panel cover
datablock StaticShapeData(JailWirePanelCover)
{
	category = "InteractableItems";
	shapeFile = "~/data/shapes/wirepanel/panelcover.dts";
	emap = false;
};

function JailWirePanelCover::Operate(%this, %obj)
{
	echo("Operate jail wire panel cover");
	%obj.dump();
	%obj.setHidden(true);
	serverPlay3D(JailPanelRemove, %obj.getTransform());
}



// Wire panel
datablock StaticShapeData(JailWirePanel)
{
	category = "InteractableItems";
	shapeFile = "~/data/shapes/wirepanel/wirepanel.dts";
	emap = false;
};

function JailWirePanel::Operate(%this, %obj)
{
	echo("Operate jail wire panel");
	serverPlay3D(JailShortCircuit, %obj.getTransform());
	
	// Deactivate all the jail laser bars that are in the same group as the jail control which has been operated
	%group = %obj.getGroup();
	%count = %group.getCount();

	for ( %i = 0; %i < %count; %i++)
	{
		// Get the next object in this group, and see if it has a datablock
		%item = %group.getObject(%i);
		if (%item.getDataBlock() !$= "")
		{
			// Make the jail bars fade out; hide when finished
			if (%item.getDataBlock().getName() $= "JailBar")
			{
				%item.startFade(1500, 0, true);
				%item.schedule(1600, setHidden, true);
			}
			// Remove the invisible wall
			else if (%item.getDataBlock().getName() $= "JailBlock")
			{
				%item.schedule(1600, setHidden, true);
			}
		}
	}
}



// Jail cell controls
datablock StaticShapeData(JailControls)
{
	category = "InteractableItems";
	shapeFile = "~/data/shapes/cardswipe/cardswipe.dts";
};

function JailControls::OnAdd(%this, %obj)
{
	%obj.setSkinName("keypad");
}

function JailControls::Operate(%this, %obj)
{
	echo("Operate jail control");
	serverPlay3D(JailActivated, %obj.getTransform());
	
	// Deactivate all the jail laser bars that are in the same group as the jail control which has been operated
	%group = %obj.getGroup();
	%count = %group.getCount();

	for ( %i = 0; %i < %count; %i++)
	{
		// Get the next object in this group, and see if it has a datablock
		%item = %group.getObject(%i);
		if (%item.getDataBlock() !$= "")
		{
			// Make the jail bars fade out; hide when finished
			if (%item.getDataBlock().getName() $= "JailBar")
			{
				%item.startFade(1500, 0, true);
				%item.schedule(1600, setHidden, true);
			}
			// Remove the invisible wall
			else if (%item.getDataBlock().getName() $= "JailBlock")
			{
				%item.schedule(1600, setHidden, true);
			}
		}
	}
}